/* 
	@Copyright (c) 2007 by Denis Riabtchik. All rights reserved. See license.txt and http://jgrouse.com for details@
	$Id: Content.java 371 2008-02-25 00:09:16Z denis.riabtchik $
*/
package jgrouse.jgdoc.elements;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import jgrouse.jgdoc.api.IContent;
import jgrouse.jgdoc.api.IContentSection;
import jgrouse.jgdoc.api.ILink;

public class Content implements IContent, Serializable
{
	private static final long serialVersionUID = -1895187347015906882L;
	private List<IContentSection> m_contentSections;
	private String m_originalString;
	
	public Content()
	{
		m_contentSections = new ArrayList<IContentSection>();
	}
	
	/* (non-Javadoc)
	 * @see jgrouse.jgdoc.elements.IContent#addContentSection(jgrouse.jgdoc.api.IContentSection)
	 */
	public IContent addContentSection(IContentSection section)
	{
		m_contentSections.add(section);
		return this;
	}
	
	/* (non-Javadoc)
	 * @see jgrouse.jgdoc.elements.IContent#addContentSection(int, jgrouse.jgdoc.api.IContentSection)
	 */
	public IContent addContentSection(int index, IContentSection section)
	{
		m_contentSections.add(index, section);
		return this;
	}
	
	public String toString()
	{
		
		StringBuffer buf = new StringBuffer("<content>");
		for (IContentSection cs : m_contentSections)
		{
			buf.append(cs.toString());
		}
		buf.append("</content>");
		return buf.toString();
	}
	
	/* (non-Javadoc)
	 * @see jgrouse.jgdoc.elements.IContent#collectLinks(java.util.List)
	 */
	public List<ILink> collectLinks(List<ILink> pList)
	{
		for (IContentSection ce : m_contentSections)
		{
			if (ce instanceof ILink)
			{
				ILink l = (ILink) ce;
				pList.add(l);
			}
		}
		return pList;
	}

	public String getOriginalString()
	{
		if (m_originalString != null)
		{
			return m_originalString;
		}
		else
		{
			StringBuffer buf = new StringBuffer();
			for (IContentSection cs : m_contentSections)
			{
				buf.append(cs.getText());
			}
			return buf.toString();
		}
	}

	public void setOriginalString(String pOriginalString)
	{
		m_originalString = pOriginalString;
	}
	
	public List<IContentSection> getSections()
	{
		return m_contentSections;
	}
}
